import { createRouter, createWebHashHistory } from "vue-router";
import { useAccount } from "../store/user";
const router = createRouter({
  history: createWebHashHistory(), //启用哈希路由模式
  routes: [
    {
      path: "/",
      redirect: "/home",
      component: () => import("../views/main.vue"),
      children: [
        {
          path: "/home",
          component: () => import("../views/home.vue"),
        },
        {
          path: "/job",
          component: () => import("../views/job.vue"),
        },
        {
          path: "/mine",
          component: () => import("../views/mine.vue"),
        },
      ],
    },
    {
      path: "/login",
      component: () => import("../views/login.vue"),
    },
    {
      path: "/setting",
      component: () => import("../views/setting.vue"),
    },
    {
      path: "/detail/:id",
      component: () => import("../views/detail.vue"),
    },
    {
      path: "/pinia",
      component: () => import("../views/pinia/index.vue"),
    },
  ],
});
router.beforeEach((to, _, next) => {
  const user = useAccount(); //注意位置
  let blackList = ["/mine", "/setting"];
  if (blackList.includes(to.path)) {
    if (user.userInfo) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});
export default router;
